var app = getApp();
Page({
  data: {
    item: [],
    sum: 0,
    PointItem: [],
    touchEnd: 0,
    touchStart: 0,
    checked: 0,
    isAll: false
  },

  PointTap: function (e) {
    var postId = e.currentTarget.dataset.postid
    let that = this
    let item = this.data.item
    item[postId].isPoint2 = !item[postId].isPoint2
    // 控制红点的显示
    var i
    var a = 0
    var PointItem = []
    for (i = 0; i < this.data.sum; i++) {
      if (item[i].isPoint2 == true) {
        a++;
        PointItem.push(item[i])
      }
    }
    that.setData({
      item: item,
      PointItem: PointItem,
      checked: a,
    })
    console.log(this.data.checked)
  },

  touchStart: function (e) {
    var that = this;
    that.setData({
      touchStart: e.timeStamp
    })
  },

  touchEnd: function (e) {
    var that = this;
    that.setData({
      touchEnd: e.timeStamp
    })
  },

  pressTap: function (e) {
    var postid = e.currentTarget.dataset.postid
    var that = this;
    var touchTime = that.data.touchStart - that.data.touchEnd;
    if (touchTime >= 600) { //自定义长按时长，单位为ms
      wx.navigateTo({
        url: '/pages/mark_2/mark_2?id=' + postid,
      })
      app.globalData.pass = this.data.item
      that.setData({
        touchEnd: 0,
        touchStart: 0
      })
    } if (touchTime < 600) {
      that.setData({
        touchEnd: 0,
        touchStart: 0
      })
    }
  },

  OnOut_FinishTap: function (event) {
    wx.redirectTo({
      url: '/pages/message/message?id=' + 2,
    })
    var that = this
    app.globalData.checked = that.data.checked
    app.globalData.list = this.data.PointItem
    
  },

  OnatAllTap: function (e) {
    var that = this
    var items = this.data.item
    var i
    var a = 0
    var PointItem = []
    for (i = 0; i < this.data.sum; i++) {
      if (that.data.isAll == false) {
        if (items[i].isPoint2 == false) {
          items[i].isPoint2 = !items[i].isPoint2
        }
      } else {
        if (items[i].isPoint2 == true) {
          items[i].isPoint2 = !items[i].isPoint2
        }
      }
      if (items[i].isPoint2 == true) {
        a++
        PointItem.push(items[i])
      }
    }
    that.setData({
      isAll: !this.data.isAll,
      item: items,
      checked: a,
      PointItem: PointItem
    })
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '淘汰名单'
    })
    let tableName = 'data'
    let query1 = new wx.BaaS.Query()
    let query2 = new wx.BaaS.Query()
    //是否属于第一志愿
    query1.compare('department1', '=', app.globalData.department)
    query1.compare('isPass1', '=', 0)
    query1.compare('isPass2', '=', 1)
    query1.compare('isPass' , '=', 0 )
    query1.compare('isUsed2', '=', 0)

    //是否是第二志愿
    query2.compare('department2', '=', app.globalData.department)
    query2.compare('isPass2', '=', 0)
    query2.compare('isPass1', '=', 0)
    query2.compare('isUsed2', '=', 0)
    query2.compare('isPass' , '=', 0 )

    let orQuery = wx.BaaS.Query.or(query1, query2)

    let Product = new wx.BaaS.TableObject(tableName)
    Product.setQuery(orQuery).limit(1000).offset(0).find().then(res => {
      console.log(res)
      this.setData({
        item: res.data.objects,
        sum: res.data.objects.length
      })
      console.log(this.data.item)
    }, err => {
      // err
    })

    this.setData({
      touchEnd: 0,
      touchStart: 0,
    })
  },


})